/* 
	该文件主要用于：
			1.定义：actions、mutations、state
			2.创建一个Vuex中最最最最最核心的人——store
			3.且store要管理：actions、mutations、state
*/
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import {nanoid} from 'nanoid';
Vue.use(Vuex);

const actions = {
    addNews(miniStore){
        // TODO: 待更正
        axios.get('http://127.0.0.1:3002/duanzi').then(
            response => {
                miniStore.commit('ADD_NEWS',response.data)
            },
            error => {
                alert(error);
            }
        )
    },
    // 年龄为奇数再加
    incrementIfOdd(miniStore,value){
        if(miniStore.state.schoolmsg.age % 2){
            miniStore.commit('INCREMENT_SCHOOL_AGE',value);
        }
    },
    // 等一等再加
    incrementWait(miniStore,value){
        setTimeout(() => {
            miniStore.commit('INCREMENT_SCHOOL_AGE',value);
        }, 1000);
    }
};
const mutations = {
    // 学校年龄+
    INCREMENT_SCHOOL_AGE(state,value){
        state.schoolmsg.age += value
    },
    // 学校年龄-
    DECREMENT_SCHOOL_AGE(state,value){
        state.schoolmsg.age -= value
    },


    ADD_NEWS(state,value){
        const newsItem = {id:nanoid(),title:value};
        state.newsList.unshift(newsItem);
    }
};
const state = {
    schoolmsg:{
        name:'河科大',
        address:'龙翔街对面',
        tel:'000-0379826',
        age:58
    },
    newsList:[
        {id:'w2378gy',title:'对花对酒,落梅长愁,十里长亭水悠悠'},
        {id:'w2de8gy',title:'容颜会老去,时光不会停'},
        {id:'w2d45hy',title:'你笑起来真像好天气'},
        
    ]
    
};
const getters = {};

export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters,
})





